package cn.com.basic.framework.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author
 */
public class RequestUtil {

    private RequestUtil() {
    }

    /**
     * 获取当前会话 request
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 设置request属性值
     * @param key
     * @param value
     */
    public static void setAttribute(String key, Object value) {
        getRequest().setAttribute(key, value);
    }

    /**
     * 判断请求是否是ajax
     * @return
     */
    public static boolean isAjax() {
        HttpServletRequest request = RequestUtil.getRequest();
        String requestType = request.getHeader("X-Requested-With");
        return "XMLHttpRequest".equalsIgnoreCase(requestType) && !"ajax".equals(request.getParameter("ace"));
    }

    /**
     *
     * @return
     */
    public static boolean fromAce() {
        return "ajax".equals(getRequest().getParameter("ace"));
    }

    /**
     * 获取请求URL
     * @return
     */
    public static String getRequestURL() {
        HttpServletRequest request = getRequest();
        //获取请求URI
        StringBuffer requestPage = request.getRequestURL();
        //获取参数
        String queryString = (request.getQueryString() == null ? "" : "?" + request.getQueryString());
        return requestPage + queryString;
    }

    /**
     * 获取请求IP地址
     * @return
     */
    public static String getIpAddr() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.contains(",")) {
            ip = ip.split(",")[0];
        }
        if (!ip.contains(".")) {
            ip = "0.0.0.0";
        }
        return ip;
    }

    /**
     * 获取请求参数值
     * @param key
     * @return
     */
    public static String getParameter(String key) {
        String parameter = getRequest().getParameter(key);
        if (parameter == null) {
            return null;
        }
        return parameter.trim();
    }

    /**
     * 获取请求参数数组值
     * @param key
     * @return
     */
    public static String[] getParameterValues(String key) {
        return getRequest().getParameterValues(key);
    }

    /**
     * 获取Long类型参数值
     * @param key
     * @return
     */
    public static Long getLongParameter(String key) {
        try {
            String strVal = getRequest().getParameter(key);
            return Long.parseLong(strVal);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取Integer类型参数值
     * @param key
     * @return
     */
    public static Integer getIntegerParameter(String key) {
        try {
            String strVal = getRequest().getParameter(key);
            return Integer.parseInt(strVal);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取BigDecimal类型参数值
     * @param key
     * @return
     */
    public static BigDecimal getBigDecimalParameter(String key) {
        try {
            String strVal = getRequest().getParameter(key);
            return new BigDecimal(strVal);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取Double类型参数值
     * @param key
     * @return
     */
    public static Double getDoubleParameter(String key) {
        try {
            String strVal = getRequest().getParameter(key);
            return Double.parseDouble(strVal);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取Boolean类型参数值
     * @param key
     * @return
     */
    public static Boolean getBooleanParameter(String key) {
        try {
            String strVal = getRequest().getParameter(key);
            if (StringUtils.isBlank(strVal)) {
                return null;
            }
            return Boolean.parseBoolean(strVal.trim());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取日期类型参数值
     * @param key
     * @param formart
     * @return
     */
    public static Date getDateParameter(String key, String formart) {
        try {
            String strVal = getRequest().getParameter(key);
            if (strVal == null) {
                return null;
            }
            return new SimpleDateFormat(formart).parse(strVal.trim());
        } catch (Exception e) {
            return null;
        }
    }

    public static List<Long> getLongParameters(String key) {
        List<Long> list = new ArrayList<>();
        String[] parameterValues = getRequest().getParameterValues(key);
        if (parameterValues == null) {
            return list;
        }
        for (String val : parameterValues) {
            try {
                list.add(Long.parseLong(val));
            } catch (Exception e) {
            }
        }
        return list;
    }


    public static List<String> getSplitParameters(String key) {
        String idsStr = RequestUtil.getParameter(key);
        if (StringUtils.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        String[] idArr = idsStr.split(",");
        return Arrays.asList(idArr);
    }

    public static List<String> getParameters(String key) {
        List<String> list = new ArrayList<>();
        String[] parameterValues = getRequest().getParameterValues(key);
        if (parameterValues == null) {
            return list;
        }
        for (String val : parameterValues) {
            if (StringUtils.isBlank(val)) {
                continue;
            }
            try {
                list.add(val);
            } catch (Exception e) {
            }
        }
        return list;
    }
}
